package com.yannan.lock.core.util;

import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class ValidateUtil {
    private static final String REG_USERNAME = "^[a-zA-Z]\\w{3,31}";
    private static final String REG_EMAIL = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
    private static final String REG_PASSWORD = "(?=^.{6,32}$)(?=.*\\d)(?=.*[a-zA-Z])(?!.*\n).*$";

    public ValidateUtil() {
    }

    public static List<String> validateMust(Object dataObj, String[] propNames, String[] displayNames) throws Exception {
        Assert.isTrue(propNames.length == displayNames.length, "参数名与显示名的个数不一致");
        ArrayList errorList = new ArrayList();

        for(int i = 0; i < propNames.length; ++i) {
            String propName = propNames[i];
            String displayName = displayNames[i];
            Object value = ModelUtil.getPropertyValue(dataObj, propName);
            if(value == null) {
                errorList.add(displayName + "(" + propName + ")是必填项");
            } else if(value instanceof String) {
                String valueStr = value.toString().replace("　", "").trim();
                if(valueStr.length() == 0) {
                    errorList.add(displayName + "(" + propName + ")是必填项");
                }
            }
        }

        return errorList;
    }

    public static void validateHTBlank(Object dataObj, String[] propNames, String[] displayNames, List<String> errorList) throws Exception {
        Assert.isTrue(propNames.length == displayNames.length, "参数名与显示名的个数不一致");

        for(int i = 0; i < propNames.length; ++i) {
            String propName = propNames[i];
            String displayName = displayNames[i];
            Object value = ModelUtil.getPropertyValue(dataObj, propName);
            if(value != null && value instanceof String) {
                String valueStr = value.toString().replace("　", "").trim();
                if(valueStr.length() > 0 && (value.toString().trim().length() != value.toString().length() || value.toString().startsWith("　") || value.toString().endsWith("　"))) {
                    errorList.add(displayName + "(" + propName + ")首尾不能为空格");
                }
            }
        }

    }

    public static List<String> validateRequestMap(Map<String, String[]> requestMap, String[] propNames, String[] displayNames) throws Exception {
        Assert.isTrue(propNames.length == displayNames.length, "参数名与显示名的个数不一致");
        List<String> errorList = new ArrayList<String>();
        for(int i = 0; i < propNames.length; ++i) {
            String propName = propNames[i];
            String displayName = displayNames[i];
            Object ov = ModelUtil.getPropertyValue(requestMap, propName);
            String[] value=new String[1];  
            if(ov == null) {
                errorList.add(displayName + "(" + propName + ")是必填项");
            } else if(ov instanceof String) {
                String valueStr = ov.toString().replace("　", "").trim();
                if(valueStr.length() == 0) {
                    errorList.add(displayName + "(" + propName + ")是必填项");
                }
            }else if(ov instanceof String[]){
            	value=(String[])ov; 
            	String valueStr = "";
            	for(int k=0;k<value.length;k++){  
            		valueStr = value[k].toString().replace("　", "").trim();
            		if(valueStr.length() > 0) break;
    	        }
            	if(valueStr.length() == 0) {
                    errorList.add(displayName + "(" + propName + ")是必填项");
                }
            }
        }
        return errorList;
    }
    
    public static boolean validateUserName(String userName) {
        return validateByRegex(userName, "^[a-zA-Z]\\w{3,31}");
    }

    public static boolean validatePassword(String password) {
        return validateByRegex(password, "(?=^.{6,32}$)(?=.*\\d)(?=.*[a-zA-Z])(?!.*\n).*$");
    }

    public static boolean validateEmail(String email) {
        return email != null && email.trim().length() != 0?email.matches("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"):false;
    }

  

    private static boolean validateByRegex(String input, String regex) {
        return input != null && input.trim().length() != 0?input.matches(regex):false;
    }
}